package net.atomtree.corrsepond.execotor;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by czk on 2016/1/2.
 */

/**
 @test
 Map<String, String> URLParam=new HashMap<String,String>();
 URLParam.put("appName","Spark%20Pi");
 URLParam.put("host","101.204.243.107");
 URLParam.put("port","4040");
 String appNameURL=appNameModeExecutor(URLParam);
 String noAppNameURL=noAppNameModeExecutor(URLParam);
 System.out.println(appNameURL);
 System.out.println(noAppNameURL);
 */
public class ApplicationExecutor {
    /**
     * this executor need you provide a param map,must contain 'host','port','appName' keys
     * host keys such as 101.204.243.107
     * port keys such as 4040
     * appName keys such as Spark%20Pi
     * such as http://101.204.243.107:4040/api/v1/applications/Spark%20Pi
     * @param URLParam contain required keys
     * @return one url and you can use this to correspond with spark
     */
    public static String appNameModeExecutor(Map<String, String> URLParam) throws Exception {
        System.out.println("host:"+URLParam.get("host"));
        System.out.println("port"+URLParam.get("port"));
        System.out.println("appName"+URLParam.get("appName"));
        String host=URLParam.get("host");
        String port=URLParam.get("port");
        String appName=URLParam.get("appName");
        if(null==host||null==port||port==appName){
            throw new Exception("application属性host port appName为null");
        }
        String restURL=MessageFormat.format("http://{0}:{1}/api/v1/applications/{2}/", host,port,appName);
        return restURL;
    }

    /**
     * this executor need you provide a param map,must contain 'host','port' keys
     * host keys such as 101.204.243.107
     * port keys such as 4040
     * such as http://101.204.243.107:4040/api/v1/
     * @param URLParam contain required keys
     * @return one url and you can use this to correspond with spark
     */
    public static String noAppNameModeExecutor(Map<String, String> URLParam){
        String restURL=MessageFormat.format("http://{0}:{1}/api/v1/applications/", URLParam.get("host"),URLParam.get("port"));
        return restURL;
    }


}
